#########################################################################
## Replication file for                                           
## "Structuring intra-party politics: a mixed-method study of ideological 
##    and hierarchical factions in parties"                 
##                                                    
## Authors: Ann-Kristin Kölln (ann-kristin.kolln@gu.se) and                                                  
##  		Jonathan Polk (jonathan.polk@svet.lu.se)
##              
## 28th June 2023
#########################################################################


# R version 4.3.0 (2023-04-21) -- "Already Tomorrow"; Platform: x86_64-apple-darwin20 (64-bit)

rm(list = ls())

# load data file 
dat3<-read.csv("PartyMembershipSurvey.csv", header =TRUE)


library(ggplot2)
library(dplyr)

#### ADDITIONAL ANALYSES REPORTED IN THE APPENDIX ####


### Appendix A

# testing for an association between activism and position within the party
cor.test(dat3$activism, dat3$any_office_N, method = "spearman")


## Generating Figure A1 and input for Table A1

#change order of categories in activism variable
dat3$activism_O <-factor(dat3$activism_O, levels = c("0 hrs", "1-5 hrs", "6-10 hrs","11-15 hrs","16-20 hrs", "21-25 hrs", "26-30 hrs", "31-35 hrs", "36-40 hrs", "41-45 hrs", "46-50 hrs", "51-55 hrs", "56-60 hrs", "61-65 hrs","66-70 hrs", "71-75 hrs","76-80 hrs", "81-85 hrs", "86-90 hrs","91-95 hrs", "96-100 hrs", "more than 100 hrs"))


#create subset without NAs
office<-dat3[c("activism_O", "any_office")]
office<-na.omit(office)

activism_office<-ggplot(office, aes (x= activism_O, fill=any_office)) + 
			geom_bar(aes(y =after_stat(count)), position = "fill")+
			xlab("number of hrs spent with party work per month") +
			scale_fill_grey() +
			ylab("proportion") +
			theme_bw()
			
jpeg("Figure_A1.jpeg", width= 8, height= 6, units='in', res=300)   			
activism_office + theme(axis.text.x = element_text(angle = 45, vjust = 0.8, hjust=0.9)) 
dev.off()


# generating input for Table A1
prop.table(table(dat3$activism_O, dat3$any_office), 1)*100
table(dat3$activism_O, dat3$any_office)



## Generating Figures A2 and A3

data_hist <- dat3 %>%
			group_by (party2, iself) %>%
			dplyr::summarise(n = n()) %>%
			dplyr::mutate(freq =n /sum(n))

histo_LR<-ggplot(data = data_hist, aes(x= iself, y = freq*100, fill = party2)) +
			geom_bar (stat= "identity", fill = "black") +
			xlab("Left-Right Self-Placement")+
			ylab("Percentage") +
			facet_wrap(~party2) +
			scale_x_continuous(breaks = seq (0, 10, 1))+
			theme_bw()+ 
			theme(legend.position = "none")


jpeg("Figure_A2.jpeg", width= 8, height= 6, units='in', res=300)        
histo_LR 
dev.off()


data_hist2 <- dat3 %>%
			group_by (party2, iself_c) %>%
			dplyr::summarise(n = n()) %>%
			dplyr::mutate(freq =n /sum(n))

histo_GT<-ggplot(data = data_hist2, aes(x= iself_c, y = freq*100, fill = party2)) +
			geom_bar (stat= "identity", fill = "black") +
			xlab("GAL-TAN Self-Placement")+
			ylab("Percentage") +
			facet_wrap(~ party2) +
			scale_x_continuous(breaks = seq (0, 10, 1))+
			theme_bw()+ 
			theme(legend.position = "none")


jpeg("Figure_A3.jpeg", width= 8, height= 6, units='in', res=300)        
histo_GT 
dev.off()


## correlation of positional issues with LR scale and with GT scale
cor.test(dat3$reduce_income, dat3$iself)
cor.test(dat3$private_health, dat3$iself)
cor.test(dat3$intervene_economy, dat3$iself)

cor.test(dat3$immigrants_adapt, dat3$iself_c)
cor.test(dat3$fewer_refugees, dat3$iself_c)
cor.test(dat3$stiffer_sentences, dat3$iself_c)





